package lib

import (
	"encoding/json"
	"fmt"
	"time"
)

type TimeN struct {
	// 爸爸的事情我全部拿过来了
	time.Time
}

var (
	DATETIME_FORMAT_PATTERN_N = "2006/01/02 15:04:05"
	DATE_FORMAT_PATTERN_N     = "2006/01/02"
)

/**
 * @author feige
 * @date 2023-10-15
 * @version 1.0
 * @desc json序列化
 */
func (t *TimeN) MarshalJSON() ([]byte, error) {
	if t.IsZero() {
		return []byte("\"\""), nil
	}
	stamp := fmt.Sprintf("\"%s\"", t.Format(DATETIME_FORMAT_PATTERN))
	return []byte(stamp), nil
}

/**
 * @author feige
 * @date 2023-10-15
 * @version 1.0
 * @desc  json反序列化
 */
func (t *TimeN) UnmarshalJSON(data []byte) error {
	var err error
	t.Time, err = time.Parse(`"`+DATETIME_FORMAT_PATTERN_N+`"`, string(data))
	if err != nil {
		t.Time, err = time.Parse(`"`+DATE_FORMAT_PATTERN_N+`"`, string(data))
	}
	return err
}

/**
 * @author feige
 * @date 2023-10-15
 * @version 1.0
 * @desc 快速打印的日期信息
 */
func (t *TimeN) String() string {
	data, _ := json.Marshal(t)
	return string(data)
}
